package com.college.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.college.commonutils.JwtUtils;
import com.college.commonutils.R;
import com.college.commonutils.ordervo.CourseWebVoOrder;
import com.college.eduservice.client.OrderClient;
import com.college.eduservice.entity.EduCourse;
import com.college.eduservice.entity.EduTeacher;
import com.college.eduservice.entity.chapter.ChapterVo;
import com.college.eduservice.entity.frontvo.CourseFrontVo;
import com.college.eduservice.entity.frontvo.CourseWebVo;
import com.college.eduservice.service.EduChapterService;
import com.college.eduservice.service.EduCourseService;
import com.college.eduservice.service.EduTeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
@CrossOrigin
public class CourseFrontController {

    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private OrderClient orderClient;

    //条件查询分页查询数据
    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public R getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> eduCoursePage = new Page<>(page,limit);
        Map<String,Object> map = courseService.getCourseFrontList(eduCoursePage,courseFrontVo);
        return R.ok().data(map);
    }

    //查询课程详情
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseList(@PathVariable String courseId, HttpServletRequest request) {
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        String id = JwtUtils.getMemberIdByJwtToken(request);
        boolean isbuyCourse = false;
        if (!id.equals("")){
            isbuyCourse = orderClient.isbuyCourse(courseId, id);
        }
        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isbuyCourse",isbuyCourse);
    }

    //根据课程id查询课程信息
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder webVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,webVoOrder);;
        return webVoOrder;
    }

}
